import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.security.PublicKey;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by 蜡笔小新丶 on 2018/4/3.
 */
public class Sell extends JFrame implements ActionListener{
    JLabel jlbArr[] = new JLabel[7],path;
    JPanel jpArr[] = new JPanel[7];
    JTextField jtfArr[] = new JTextField[7];
    JFileChooser imge;
    JButton jb1,jb2,AddImage;
    String [] arr = {"品    名","产    地","种    类","报    价","联系方式","商品图片","备    注"};

    public Sell(){
        for(int i=0;i<jlbArr.length;i++){
            jlbArr[i] = new JLabel(arr[i]);//实例化标签
            jpArr[i] = new JPanel();//实例化面板
            jtfArr[i] = new JTextField(40);//实例化输入框
        }
        FileNameExtensionFilter filter =
                new FileNameExtensionFilter(
                        "JPG & PNG Images", "jpg", "png");
        path = new JLabel();
        path.setPreferredSize(new Dimension(150,30));
        imge = new JFileChooser();
        imge.setFileFilter(filter);
        imge.setPreferredSize(new Dimension(500,300));
        //实例化按钮
        jb1 = new JButton("上架");
        jb2 = new JButton("取消");
        AddImage = new JButton("添加图片");
        //为按钮添加监听
        jb1.addActionListener(this);
        jb2.addActionListener(this);
        AddImage.addActionListener(this);
        //将组件加入面板
        this.setLayout(new FlowLayout(FlowLayout.CENTER,250,10));
        JLabel title = new JLabel("玉石上架");
        title.setFont(new Font("宋体",Font.BOLD,20));
        this.add(title);
        for(int i=0;i<jpArr.length;i++){
            if(i!=5) {
                jpArr[i].add(jlbArr[i]);
                jpArr[i].add(jtfArr[i]);
            }else {
                jpArr[i].setLayout(new FlowLayout(FlowLayout.CENTER,10,0));
                jpArr[i].add(jlbArr[i]);
                jpArr[i].add(path);
                jpArr[i].add(AddImage);
            }
            this.add(jpArr[i]);
        }

        JPanel jp = new JPanel();
        jp.setLayout(new FlowLayout(FlowLayout.CENTER,100,5));
        jp.add(jb1);
        jp.add(jb2);
        this.add(jp);

        //设置窗体
        this.setTitle("商品上架");//窗体标签
        this.setSize(400, 420);//窗体大小
        this.setLocationRelativeTo(null);//在屏幕中间显示(居中显示)
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);//退出关闭JFrame
        this.setVisible(true);//显示窗体

        //锁定窗体
        this.setResizable(false);
    }

    public static void main(String[] args) {
        Sell sell = new Sell();
    }

    @Override
    public void actionPerformed(ActionEvent e) {

        if(e.getActionCommand() == "上架"){
            try {
            //判断输入值是否为空
            if(!jtfArr[0].getText().equals("") &&
                    !jtfArr[1].getText().equals("") &&
                    !jtfArr[2].getText().equals("") &&
                    !jtfArr[3].getText().equals("") &&
                    !jtfArr[4].getText().equals("") &&
                    imge.getSelectedFile() != null &&
                    !jtfArr[6].getText().equals("")) {

                    FileInputStream fi = new FileInputStream(imge.getSelectedFile());
                    byte []im = new byte[(int) imge.getSelectedFile().length()];//保存图片的二进制数组
                    fi.read(im);

                //获取当前日期时间
                SimpleDateFormat df = new SimpleDateFormat(
                        "yyyyMMddHHmmss");
                //创建玉石对象
                Jade jade = new Jade(df.format(new Date()),ExInform.getU().getUserNum(), jtfArr[0].getText(), jtfArr[1].getText(), jtfArr[2].getText(), jtfArr[3].getText(), jtfArr[4].getText(),im, jtfArr[6].getText());
                //调用数据库并输出提示消息
               if(ExInform.addJade(jade)) {
                   JOptionPane.showMessageDialog(null, "上架成功！", "提示消息", JOptionPane.WARNING_MESSAGE);
                   new Home(ExInform.getU());
                   this.dispose();
               }else
                   JOptionPane.showMessageDialog(null, "输入的信息不符合要求！", "提示消息", JOptionPane.WARNING_MESSAGE);
            }else
                JOptionPane.showMessageDialog(null, "上架失败，录入信息不可为空！", "提示消息", JOptionPane.WARNING_MESSAGE);//信息为空消息
        } catch (FileNotFoundException e1) {
                e1.printStackTrace();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
        if(e.getActionCommand()=="添加图片"){
            int A = imge.showOpenDialog(null);
            if(A==0)
                path.setText(imge.getSelectedFile().getAbsolutePath());
        }
        if(e.getActionCommand() == "取消"){
            new Home(ExInform.getU());
            this.dispose();
        }
    }
}
